//
//  YWHomeController.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/7/25.
//

#import "YWHomeController.h"
#import "YWPuzzleController.h"
#import "YWEditController.h"
#import "TZImagePickerController.h"
#import "YWQRCodeController.h"
#import "YWDTFaceController.h"
#import "YWTakePhotoController.h"
#import "YWTakeVideoController.h"
//#import "YWTakeVideoController2.h" //失败了所以暂时舍弃
#import "YWTakePhotoAndVideoController.h"


@interface YWHomeController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,YWEditControllerDelegate,TZImagePickerControllerDelegate,YWPuzzleControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)puzzleClick:(UIButton *)sender;
- (IBAction)photoClick:(UIButton *)sender;
- (IBAction)QRCodeBtnClick:(UIButton *)sender;
- (IBAction)faceBtnClick:(UIButton *)sender;

- (IBAction)takePhotoBtnClick:(UIButton *)sender;
- (IBAction)takeVideoBtnClick:(UIButton *)sender;
//- (IBAction)takeVideoBtnClick2:(UIButton *)sender; //失败了所以暂时舍弃
- (IBAction)takePhotoAndVideoClick:(UIButton *)sender;


@end

@implementation YWHomeController

/*
 //多选相册使用的网址：
 https://github.com/banchichen/TZImagePickerController    (github)
 https://www.jianshu.com/p/7cf92b19014d    (简书详细介绍)
 https://blog.csdn.net/zuoweixiaoduzuozuo/article/details/83108282 (简书简单介绍)
 */


- (void)viewDidLoad {
    [super viewDidLoad];
    
//    YWDTFaceController *vc = [[YWDTFaceController alloc] initWithNibName:@"YWDTFaceController" bundle:nil];
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//    
//    nav.modalPresentationStyle = 0;
//    [self presentViewController:nav animated:YES completion:nil];
    
}


- (IBAction)faceBtnClick:(UIButton *)sender {
    
    YWDTFaceController *vc = [[YWDTFaceController alloc] initWithNibName:@"YWDTFaceController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    nav.modalPresentationStyle = 0;
    [self presentViewController:nav animated:YES completion:nil];
    
    
}

- (IBAction)QRCodeBtnClick:(UIButton *)sender {
    
    YWQRCodeController *vc = [[YWQRCodeController alloc] initWithNibName:@"YWQRCodeController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    nav.modalPresentationStyle = 0;
    [self presentViewController:nav animated:YES completion:nil];
}

- (IBAction)photoClick:(UIButton *)sender {
    //从系统相册当中选择一张图片
    //1.弹出系统相册
    UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
    //设置照片的来源
    pickerVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    pickerVC.delegate = self;
    //modal出系统相册
    [self presentViewController:pickerVC animated:YES completion:nil];
}

//当选择某一个照片时,会调用这个方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    NSLog(@"%@",info);
    UIImage *image  = info[UIImagePickerControllerOriginalImage];
   
    //取消弹出的系统相册
    [self dismissViewControllerAnimated:YES completion:nil];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        YWEditController *vc = [[YWEditController alloc] initWithNibName:@"YWEditController" bundle:nil];
        vc.delegate = self;
        vc.image = image;
        vc.modalPresentationStyle = 0;
        [self presentViewController:vc animated:YES completion:nil];
        
    });
}

- (IBAction)puzzleClick:(UIButton *)sender {
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:3 delegate:self];
    imagePickerVc.minImagesCount = 3;
    
    
//    imagePickerVc.modalPresentationStyle = 0;
    
//    imagePickerVc.navigationBarHidden = YES; //它内部做了修改了、所以这些操作都没用
//    imagePickerVc.navigationBar.hidden = YES;
//    imagePickerVc.navigationBar.alpha = 0;
    
    
    // 你可以通过block或者代理，来得到用户选择的照片.
    MJWeakSelf
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
//这里跳去拼图VC、并把选择好的photos和assets传过去
        YWPuzzleController *vc = [[YWPuzzleController alloc] initWithNibName:@"YWPuzzleController" bundle:nil];
        vc.delegate = self;
        vc.modalPresentationStyle = 0;
        vc.photos = photos;
        vc.assets = assets;
        [weakSelf presentViewController:vc animated:YES completion:nil];
        
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
    
//            [self addChildViewController:imagePickerVc];
//            [self.view addSubview:imagePickerVc.view];
//            imagePickerVc.view.frame = CGRectMake(50, 150, 300, 400);
    
}

-(void)editController:(YWEditController *)editController newImage:(UIImage *)newImage{
    
    self.imageView.image = newImage;
}

-(void)puzzleController:(YWPuzzleController *)puzzleController newImage:(UIImage *)newImage{
    
    self.imageView.image = newImage;
}


//- (IBAction)takeVideoBtnClick2:(UIButton *)sender {
//
//    YWTakeVideoController2 *vc = [[YWTakeVideoController2 alloc] initWithNibName:@"YWTakeVideoController2" bundle:nil];
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//
//    nav.modalPresentationStyle = 0;
//    [self presentViewController:nav animated:YES completion:^{
//    }];
//
//}

- (IBAction)takeVideoBtnClick:(UIButton *)sender {
    
    
    
    YWTakeVideoController *vc = [[YWTakeVideoController alloc] initWithNibName:@"YWTakeVideoController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    nav.modalPresentationStyle = 0;
    [self presentViewController:nav animated:YES completion:^{
    }];
    
    
    
}

- (IBAction)takePhotoBtnClick:(UIButton *)sender {
    
//    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
//
//    NSInteger orientation = [UIDevice currentDevice].orientation;
////
////
//    YWLog(@"orientation:%ld",orientation);
    
    
    YWTakePhotoController *vc = [[YWTakePhotoController alloc] initWithNibName:@"YWTakePhotoController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    nav.modalPresentationStyle = 0;
    [self presentViewController:nav animated:YES completion:^{
//        [[UIDevice currentDevice]setValue:@(beforeOrientation)forKey:@"orientation"];
    }];
    
    
}


- (IBAction)takePhotoAndVideoClick:(UIButton *)sender {
    
    
    
    YWTakePhotoAndVideoController *vc = [[YWTakePhotoAndVideoController alloc] initWithNibName:@"YWTakePhotoAndVideoController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    nav.modalPresentationStyle = 0;
    [self presentViewController:nav animated:YES completion:^{
        
    }];
    
    
}
@end
